<?php
/**
 * Joomla! 1.5 component ArticleCashRewards
 *
 * @version $Id: rejected.php 2009-05-10 01:47:01 svn $
 * @author Maverick
 * @package models
 * @subpackage ArticleCashRewards
 * @license GNU/GPL
 * Website: http://www.boddunan.com
 *
 * Review and approve the articles and assign points and cash rewards.
 * Users can see their articles and the points/cash rewards assigned in the front-end module.
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

// Import Joomla! libraries
jimport('joomla.application.component.model');

if(!class_exists('ContentHelperRoute')) require_once (JPATH_SITE . '/components/com_content/helpers/route.php');  

class ArticleCashRewardsModelRejected extends JModel {

	var $_total = null; 
	var $_pagination = null;
	
	function __construct(){
	
		parent::__construct();
		global $mainframe, $option;
	
		// Get pagination request variables
		$limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = $mainframe->getUserStateFromRequest($option.'.limitstart', 'limitstart', 0, 'int');
		
		// In case limit has been changed, adjust it
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
		
		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
	}

    function getRejectedArticles() {
	    
    	global $mainframe, $option;
    	
    	// Get a database object
		$db =& JFactory::getDBO();
		$user =& JFactory::getUser();
		$filter_order		= $mainframe->getUserStateFromRequest( "$option.filter_order",		'filter_order',		'a.created',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( "$option.filter_order_Dir",	'filter_order_Dir',	'DESC',		'word' );
		
		$orderby 	= ' ORDER BY '. $filter_order .' '. $filter_order_Dir;

		$query = 'SELECT a.id, a.alias, a.title, a.sectionid, a.catid, s.title AS section, cc.title AS category,'.
			' s.access AS sec_access, cc.access AS cat_access, s.published AS sec_state, cc.published AS cat_state,'.
			' u.username AS author, a.created_by_alias, a.created_by, acr.points, acr.cash, acr.remarks,' .
			' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug,'.
			' CASE WHEN CHAR_LENGTH(cc.title) THEN CONCAT_WS(":", cc.id, cc.title) ELSE cc.id END as catslug'.
			' FROM #__content AS a' .
			' LEFT JOIN #__categories AS cc ON cc.id = a.catid' .
			' LEFT JOIN #__sections AS s ON s.id = a.sectionid' .
			' LEFT JOIN #__users AS u ON u.id = a.created_by' .
			' LEFT JOIN ' . TABLE_ACR_CONTENT  . ' AS acr ON acr.article_id = a.id' .
			' WHERE a.state = 0 AND acr.status = 2'.$orderby;
//echo $query;

		$db->setQuery($query, $this->getState('limitstart'), $this->getState('limit'));
		$rows = $db->loadObjectList();

		$lists['order_Dir']	= $filter_order_Dir;
		$lists['order']		= $filter_order;
		
		$result->rows = $rows;
		$result->lists = $lists;
		
		return $result;
    }
    
    function getTotal(){

	    if (empty($this->_total)) {
	    
			$db =& JFactory::getDBO();

			$query = 'SELECT COUNT(a.id) FROM ' . TABLE_ACR_CONTENT  . ' acr '.
				' LEFT JOIN  #__content a ON acr.article_id =  a.id'.
				' WHERE a.state = 0 and acr.status=2';
			$db->setQuery( $query );
			$total = $db->loadResult();
		}			
		return $total;
	}
	  
	function getPagination(){
        global $mainframe,$option;
		$pnls = JRequest::getVar( 'pnlimitstart', 'null');
		
		if($pnls == 'null'){
		    jimport('joomla.html.pagination');
		    $this->_pagination = new JPagination($this->getTotal(), 0, $this->getState('limit') );
		    $this->setState('limitstart', 0);
            $mainframe->setUserState("$option.filter_order", 'a.created');
		}

		// Load the content if it doesn't already exist
		if (empty($this->_pagination)) {
		    jimport('joomla.html.pagination');
		    $this->_pagination = new JPagination($this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}

		return $this->_pagination;
	}    

}
?>